// Analytics Module v1.4.1 class Analytics { constructor() { this.events = []; this.initialized = false; this.userId = null; } async initialize() { if (this.initialized) return; this.userId = this._generateUserId(); this.initialized = true; setInterval(() => this._processQueue(), 30000); } trackEvent(category, action, label = null, value = null) { if (!this.initialized) { console.warn('Analytics not initialized'); return; } this.events.push({ timestamp: Date.now(), category, action, label, value, userId: this.userId, sessionId: this._getSessionId() }); } async _processQueue() { if (this.events.length === 0) return; const batch = this.events.splice(0, 50); try { await this._sendEvents(batch); } catch (error) { console.error('Failed to send analytics:', error); this.events.unshift(...batch); } } async _sendEvents(events) { const response = await fetch('https://analytics.example.com/collect', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ events, timestamp: Date.now(), version: '1.4.1' }) }); if (!response.ok) { throw new Error(`Analytics error: ${response.status}`); } } _generateUserId() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => { const r = Math.random() * 16 | 0; return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16); }); } _getSessionId() { if (!this._sessionId) { this._sessionId = Date.now().toString(36) + Math.random().toString(36).substr(2); } return this._sessionId; } } export const analytics = new Analytics();